package november.queue;

import java.util.Scanner;

/**
 * @author huangxin
 * @date 2019/11/13-22:05
 */
public class CircleArrayQueueTest {
    public static void main(String[] args) {
        test2();
    }

    public static void test2() {
        ArrayQueue3 arrayQueue = new ArrayQueue3(5);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列的头数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.print("输入一个数:");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int result = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int result = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
        System.out.println("程序退出");
    }

}

class ArrayQueue3 {
    private int maxSize;
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;


    public ArrayQueue3(int maxSize) {
        //队列中有一个位置需为空,区分头尾
        arr = new int[maxSize];
        this.maxSize = maxSize;
        this.front = 0;
        this.rear = 0;
    }


    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\t", i % maxSize, arr[i % maxSize]);
            if (i % maxSize == rear) {
                break;
            }
        }
        System.out.println();
    }

    public void addQueue(int value) {
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }

    /**
     * 判断是否满
     *
     * @return
     */
    public boolean isFull() {
        // or (尾+1)%maxsize == 头
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public int size() {
        return (rear - front + maxSize) % maxSize;
    }
}

